package com.lorentzos.flingswipe;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by dionysis_lorentzos on 6/8/14
 * for package com.lorentzos.swipecards
 * and project Swipe cards.
 * Use with caution dinausaurs might appear!
 */
public class CustomPager extends PageSlider implements Component.TouchEventListener {
    private float x1,x2;
    private onFlingListener mFlingListener;
    public CustomPager(Context context) {
        super(context);
        setTouchEventListener(this);
        setPageSwitchTime(10);
    }
    public void setFlingListener(onFlingListener onFlingListener) {
        this.mFlingListener = onFlingListener;
    }
    public CustomPager(Context context, AttrSet attrSet) {
        super(context, attrSet);
        setTouchEventListener(this);
        setPageSwitchTime(10);
    }

    public CustomPager(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setTouchEventListener(this);
        setPageSwitchTime(10);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        MmiPoint point1 = touchEvent.getPointerPosition(touchEvent.getIndex());
        boolean isMove=false;
        switch (touchEvent.getAction()){
            case TouchEvent.PRIMARY_POINT_DOWN:
                x1=point1.getX();
                break;
            case TouchEvent.POINT_MOVE:
                x2=point1.getX();
                if(mFlingListener!=null){
                    mFlingListener.onScroll(x2);
                }
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                if (x2-x1 > 0) {
                    isMove=true;
                    if(mFlingListener!=null){
                        mFlingListener.onRightCardExit(x2);
                    }
                } else if (x2-x1 < 0) {
                    isMove=true;
                    if(mFlingListener!=null){
                        mFlingListener.onLeftCardExit(x2);
                    }
                }
                if(isMove){
                    selectInit();
                }
                 break;
            default:break;
        }
        return true;
    }

    public interface onFlingListener {
        void removeFirstObjectInAdapter();
        void onLeftCardExit(Object dataObject);
        void onRightCardExit(Object dataObject);
        void onAdapterAboutToEmpty(int itemsInAdapter);
        void onScroll(float scrollProgressPercent);
    }
    public void selectRight(){
        selectInit();
        if(mFlingListener!=null){
            mFlingListener.onRightCardExit(x2);
        }
    }
    public void selectLeft(){
        selectInit();
        if(mFlingListener!=null){
            mFlingListener.onLeftCardExit(x2);
        }
    }
    public void selectInit(){
        if(mFlingListener!=null){
            mFlingListener.removeFirstObjectInAdapter();
        }
        if(getCurrentPage()+1>getProvider().getCount()-1){
            if(mFlingListener!=null){
                mFlingListener.onAdapterAboutToEmpty(getCurrentPage());
            }
        }
        setCurrentPage(getCurrentPage()+1);
    }

}
